package net.csdn.business.auth.authing;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import net.csdn.business.auth.component.CustomAuthExceptionEntryPoint;
import net.csdn.business.auth.service.CustomUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;


/**
 * @Desc: authing登录配置类
 * @ClassName: AuthingSecurityConfigurer
 * @Author: yourleige@163.com
 * @Date: 2022/9/26 15:45
 */
@Getter
@Setter
public class AuthingSecurityConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

	@Autowired
	private ObjectMapper objectMapper;

	/*@Autowired
	private AuthenticationSuccessHandler mobileLoginSuccessHandler;*/

	@Autowired
	private CustomUserDetailsService userDetailsService;

	@Override
	public void configure(HttpSecurity http) {
		AuthingAuthenticationFilter authingAuthenticationFilter = new AuthingAuthenticationFilter();
		authingAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
		//authingAuthenticationFilter.setAuthenticationSuccessHandler(mobileLoginSuccessHandler);
		authingAuthenticationFilter.setAuthenticationEntryPoint(new CustomAuthExceptionEntryPoint(objectMapper));
		AuthingAuthenticationProvider authingAuthenticationProvider = new AuthingAuthenticationProvider();
		authingAuthenticationProvider.setUserDetailsService(userDetailsService);
		http.authenticationProvider(authingAuthenticationProvider).addFilterAfter(authingAuthenticationFilter,
				UsernamePasswordAuthenticationFilter.class);
	}

}
